package org.six.background.controller;

import org.six.background.pojo.User;
import org.six.background.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@ResponseBody
@RequestMapping("/Account")
public class AccountController {
    @Autowired
    private AccountService accountService;


    //查询账户
    @RequestMapping("/accountAll")
    public Map userAll(int page,int pagesize){
        Map map = accountService.userAll(page, pagesize);
        return map;
    }

    //添加账户
    @RequestMapping("/insert")
    public String insert(User user){
        return accountService.insert(user);
    }


    //编辑信息
    @RequestMapping("/update")
    public String updateus(int usid,String usname,String usphone,String uscard){
        return accountService.updateus(usid,usname,usphone,uscard);
    }



    @RequestMapping("/toux")//第一步，在页面加载前传送图片地址
    public String toux(int usid){
        return accountService.toux(usid);
    }


    //修改头像
    @RequestMapping("/updatetp")//第二步，如果上传图片，返回图片地址
    public String updatetp(@RequestParam("file") MultipartFile file,HttpServletRequest request){
        return accountService.updatetp(file,request);
    }


    @RequestMapping("/updateuspass")//如果有头像，修改密码和头像
    public String uspass(HttpServletRequest request,@RequestParam("usid") int usid,@RequestParam("uspass") String uspass,
                         @RequestParam("newurl")String newurl,@RequestParam("oldurl")String oldurl){
        System.out.println(request);
        System.out.println(usid);
        System.out.println(uspass);
        System.out.println(newurl);
        System.out.println(oldurl);
        return accountService.uspass(request,usid,uspass,newurl,oldurl);
    }


    @RequestMapping("/deupdateuspass")//仅根据id修改头像
    public String uspass(HttpServletRequest request,@RequestParam("usid") int usid,
                         @RequestParam("newurl")String newurl,@RequestParam("oldurl")String oldurl){
        System.out.println(request);
        System.out.println(usid);
        System.out.println(newurl);
        System.out.println(oldurl);
        return accountService.uspass(request,usid,newurl,oldurl);
    }

    @RequestMapping("/updatemima")//根据id修改密码
    public String uspass(int usid,String uspass){
        return accountService.uspass(usid,uspass);
    }

}
